package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;

//    处理前台的“根据指定时间段,获取成员数据”请求
    @RequestMapping("/getMemberByTimeQuantum")
    public Result getMemberByTimeQuantum(@RequestBody  Map map) throws Exception {
//        获取当前时间，给起始时间变量beginDate作为默认值
        String beginDate = DateUtils.parseDate2String (DateUtils.getToday ());
//        获取当前时间一年后的时间，给终止时间变量endDate作默认值
        String[] strs = beginDate.split ("-");
        String year = String.valueOf (Integer.parseInt (strs[0]) + 1);
        String endDate = year+"-"+strs[1]+"-"+strs[2];

//        若前台传过来的beginDate、endDate不为空，则赋值给变量beginDate、endDate
        if( map.get ("beginDate") != null && !map.get ("beginDate").toString ().equals ("")){
            beginDate = (String) map.get ("beginDate");
        }
        if( map.get ("endDate") != null && !map.get ("endDate").toString ().equals ("")){
            endDate = (String) map.get ("endDate");
        }

        try {
            Map<String, Object> data = memberService.getMemberByTimeQuantum (beginDate, endDate);
            Result result = new Result (true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,data);
            return result;
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
}
